package com.easonji.common.base.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * @ClassName: BaseService
 * @Description: Service抽象接口
 * @Author: LiuJun
 * @date: 2018/07/07 22:12
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
public abstract class AbstractBaseService<T> {

    @SuppressWarnings("unchecked")
    protected Object warp(Object obj) {
        if (obj instanceof List) {
            JSONArray result = new JSONArray();
            List<T> list = (List<T>) obj;
            for (T t : list) {
                result.add(warp2Json(t));
            }
            return result;
        } else if (obj instanceof Object) {
            T t = (T) obj;
            return warp2Json(t);
        } else {
            return obj;
        }
    }

    /**
     * 实体类转换页面显示结果
     *
     * @param t
     *            实体类
     * @return 页面显示数据
     */
    protected abstract JSONObject warp2Json(T t);
}
